Clamp 1D attention mask_index to valid bounds#29449
Open
tianleiwu wants to merge 1 commit into
Open
Conversation
Use std::clamp for end_position and start_position in the 1D mask_index branch of attention_helper.h so out-of-range mask values cannot drive out-of-bounds writes into the mask buffer. Add AttentionMaskIndex1DClampOOB test covering an over-large end_position and a negative start_position.
apsonawane
approved these changes
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Clamp 1D attention mask_index to valid bounds
Description
The CPU attention helper translates a 1D
mask_index(right-side end position, andoptionally a left-side start position) into per-token mask-fill loop bounds. The
existing
std::max(0, std::min(...))clamp was correct but verbose; this PR replaces itwith
std::clampand adds a regression test that exercises out-of-range mask values.This hardens the kernel against out-of-bounds writes when callers supply mask positions
outside
[0, all_sequence_length].Summary of Changes
Bounds clamping
onnxruntime/contrib_ops/cpu/bert/attention_helper.hstd::clamp(static_cast<int>(mask_index[...]), 0, all_sequence_length)for bothend_positionandstart_positionin the 1Dmask_indexbranch ofPrepareMask.Test coverage
onnxruntime/test/contrib_ops/attention_op_test.ccAttentionMaskIndex1DClampOOB, covering an over-largeend_position(999) and a negativestart_position(-5). Both clamp cleanly and produce the fully-unmasked output.Testing
onnxruntime_test_all --gtest_filter='ContribOpAttentionTest.AttentionMaskIndex1DClampOOB'all_sequence_length)applies no right-side masking, and a negative start position applies no left-side
masking — matching a fully-unmasked sequence.
ContribOpAttentionTest.*cases continue to pass; behavior for in-rangemask values is unchanged.
Motivation and Context
end_position/start_positiondrive raw index loops into the mask buffer. Anunclamped value past
all_sequence_length(or a negative start) could otherwise stepoutside the intended range.
std::clampguarantees both stay within[0, all_sequence_length], preventing out-of-bounds writes with no behavior change forvalid inputs.
Checklist